module ApplicationHelper


	def generate_qiniu_upload_token
		put_policy=Qiniu::Auth::PutPolicy.new("chenxiyue") #参数是七牛bucket名
		# put_policy.scope!("chenxiyue",key)
		put_policy.return_body={
			fname: '$(fname)',
			hash: '$(etag)',
			title:  '$(x:title)'
		}.to_json
		put_policy.return_url= upload_callback_user_url  #上传成功后回调的URL
		Qiniu::Auth.generate_uptoken(put_policy)
	end

	def qiniu_image_url(img, format = :raw)
		url="http://7xsr0z.com2.z0.glb.qiniucdn.com/#{img.qiniu_hash}"    #为了方便硬编码七牛的域名

		case format
		when :square
			url << '?imageView2/1/w/300/h/300/q/90'
		when :preview
			url << '?imageView2/2/w/1000/h/1000/q/90'
		when :raw
			url << "?attname=#{img.filename}" #以原文件名进行下载
		else
			url
		end
	end

	def qiniu_image_by_hash(hash, format = :raw)
		url="http://7xsr0z.com2.z0.glb.clouddn.com/#{hash}"    #为了方便硬编码七牛的域名

		case format
			when :square
				url << '?imageView2/1/w/300/h/300/q/90'
			when :preview
				url << '?imageView2/2/w/1000/h/1000/q/90'
			else
				url
		end
	end

end
